package connect4client.core;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import messages.Message;

import connect4client.core.Controller;

public class MessageConsumer implements Runnable {

	public MessageConsumer(Controller controller) {
		this.controller = controller;
		new Thread(this, "MessageConsumer").start();
	}
	
	public void run() {
		while(true){
			try {					
				Message message = queue.take();
				controller.forward(message);				
			} 
			catch (InterruptedException e) {			
				e.printStackTrace();
			}	
		}
	}

	
	public void putMessage(Message message){
		try {
			this.queue.put(message);
			System.err.println("Received " + message);
		} catch (InterruptedException e) {			
			e.printStackTrace();
		}
	}
	
	private BlockingQueue<Message> queue = new LinkedBlockingQueue<Message>();
	private Controller controller;
}


